package com.jxjk.reportwork;

import com.jxjk.reportwork.biz.BusinessTripBiz;
import com.jxjk.reportwork.biz.WorkReportBiz;
import com.jxjk.reportwork.model.ReportConfig;
import com.jxjk.reportwork.model.WorkMonth;
import com.jxjk.reportwork.util.PropertyUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 * java -jar hibernatedemo-1.0.0-SNAPSHOT-jar-with-dependencies.jar
 * -w D:\work\建信金科\202405报工\202408报工 -c gb2312
 *
 * @author fanyusong@126.com
 * @date 2024/9/11 12:10
 */
@Data
@Slf4j
public class ReportMain {


    private static final SessionFactory sessionFactory;

    // 创建sessionFactory对象
    static {
        try {
            // 对应的hibernate基本配置信息和对象关系映射信息
            Configuration configuration = new Configuration();
            // 找的hibernate.cfg.xml配置文件
            configuration.configure();
            // 4.0之前不需要使用serviceRegistry  直接构建sessionFactory  configuration.buildSessionFactory()
            // 4.0之后创建serviceRegistry,所有的配置和服务都要在该对象中注册才可以生效
            // 4.3之前使用该方式new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }

    }

    // 创建session对象
    public static Session getSession() throws HibernateException {
        return sessionFactory.openSession();
    }


    public static void main(final String[] args) throws Exception {
        log.info("===报工对账开始===");
        //解析命令行参数
        PropertyUtil.parseCommandLine(args);
        final Session session = getSession();
        // 开启事务
        Transaction transaction = session.beginTransaction();
        try {
            //读取配置文件
            ReportConfig reportConfig = PropertyUtil.loadConfig();
            //得到工作日列表
            PropertyUtil.calcWorkDateList(reportConfig);

            //===报工对账===
            WorkReportBiz workReportBiz = new WorkReportBiz();
            workReportBiz.setReportConfig(reportConfig);
            workReportBiz.setSession(session);
            //读取报工计划excel、报工明细excel
            workReportBiz.loadExcel();
            //报工对账，找出差异(多帐、少帐)
            workReportBiz.handleWorkReport();


            //===考勤调整===
            BusinessTripBiz businessTripBiz = new BusinessTripBiz();
            businessTripBiz.setSession(session);
            businessTripBiz.setReportConfig(reportConfig);
            //读取差旅明细单
            businessTripBiz.loadExcel();
            //分割差旅单，不允许一个差旅单跨月
            businessTripBiz.splitBusinessTrip();
            WorkMonth workMonth = new WorkMonth();
            //设置工作月份，比如2024.8月，会生成出开始日期=2024.8.1日，结束日期=2024.8.31日
            workMonth.initDate(reportConfig.getWorkMonth());
            //生成考勤调整单
            businessTripBiz.genAttendanceAdjust(workReportBiz.getReportPlanInExcelDOList(), workMonth);

            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            log.error("操作数据库出现异常", e);
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
            if (sessionFactory != null && !sessionFactory.isClosed()) {
                sessionFactory.close();
            }
        }
        log.info("===报工对账完成===");
    }


}